<?php
namespace app\credit\service;
/**
 * 商品操作
 */
class ShopService extends \app\base\service\BaseService {

    private $model = 'credit/CreditOrder';


    /**
     * 添加收藏
     * @param $userId
     * @param $data
     * @return bool
     */
    public function addFollow($userId, $data) {
        if(empty($data['credit_id'])) {
            return $this->error('商品参数有误!');
        }
        $followInfo = target('shop/ShopFollow')->getWhereInfo(['app' => 'shop', 'has_id' => $data['credit_id']]);
        if(!empty($followInfo)) {
            if(!target('credit/Credit')->where(['credit_id' => $data['credit_id']])->setDec('favorite', 1)) {
                return $this->error('取消收藏失败！');
            }
            if(!target('shop/ShopFollow')->where(['app' => 'shop', 'has_id' => $data['credit_id']])->delete()) {
                return $this->error('取消收藏失败！');
            }else {
                return $this->success('商品已取消收藏！');
            }
        }
        $shopInfo = target('credit/Credit')->getInfo($data['credit_id']);
        if(empty($shopInfo) || !$shopInfo['status']) {
            return $this->error('该商品不存在!');
        }
        if($shopInfo['up_time']) {
            if($shopInfo['up_time'] > time()) {
                return $this->error('该商品还未上架!');
            }
        }
        if($shopInfo['down_time']) {
            if($shopInfo['down_time'] < time()) {
                return $this->error('该商品已下架!');
            }
        }

        if(!target('credit/Credit')->where(['credit_id' => $data['credit_id']])->setInc('favorite', 1)) {
            return $this->error('取消收藏失败！');
        }

        $data = [];
        $data['user_id'] = $userId;
        $data['app'] = 'credit';
        $data['has_id'] = $shopInfo['credit_id'];
        $data['time'] = time();
        $data['title'] = $shopInfo['title'];
        $data['image'] = $shopInfo['image'];
        $data['price'] = $shopInfo['sell_price'];
        if(!target('shop/ShopFollow')->add($data)) {
            return $this->error('收藏商品失败！');
        }

        return $this->success('商品收藏成功!');
    }



    /**
     * 支付检查
     * @param $orderRows
     * @return bool
     */
    public function checkPay($orderRows) {
        return false;
    }
}

